package com.zatech.cgnci.project.service.impl;

import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.dto.FileUploadDTO;
import com.zatech.cgnci.project.entity.CgnciAccessoryDO;
import com.zatech.cgnci.project.entity.UserEntity;
import com.zatech.cgnci.project.service.CgnciAccessoryService;
import com.zatech.cgnci.project.service.IMosFileService;
import com.zatech.cgnci.project.vo.CgnciAccessoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * @author EDY
 * @version 1.0
 * Create by 2024/12/19 15:14
 */
@Slf4j
@Service
public class MosFileServiceImpl implements IMosFileService {

    @Resource
    CgnciAccessoryService cgnciAccessoryService;

    @Override
    public CgnciAccessoryVO uploadFileByMos(FileUploadDTO dto) {
        // 获取当前登录用户
        UserEntity user = UserContext.getUser();
        String fileName = dto.getFile().getOriginalFilename();
        String fileType = getSuffix(fileName);
        String fileId = UUID.randomUUID().toString().replace("-", "") + "." + fileType;
        CgnciAccessoryDO accessoryDO = CgnciAccessoryDO.builder()
                .accType(dto.getAccType()).uploadNode(dto.getUploadNode())
                .fileId(fileId).fileName(fileName).fileType(fileType).uploadTime(new Date())
                .status("1").msg("成功")
                .createUser(user.getNameAndCode()).updateUser(user.getNameAndCode())
                .build();
        cgnciAccessoryService.save(accessoryDO);
        return ConvertUtils.convert(accessoryDO, CgnciAccessoryVO.class);
    }

    @Override
    public String downFileByMos(Long accId) {
        return "https://test-oss.laidianli.com/images/2024/03/20/17108988665793828.jpg";
    }

    private String getSuffix(String fileName) {
        int index = fileName.lastIndexOf('.');
        return fileName.substring(index + 1);
    }
}
